/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cn.ekuma.epos.datalogic.define.dao;

import cn.ekuma.data.dao.I_DataLogic;
import cn.ekuma.data.dao.ModifiedLogDAO;
import cn.ekuma.epos.db.table.I_ProductCustomerCategoryPrice;

import com.openbravo.bean.CustomerCategory;
import com.openbravo.bean.Product;
import com.openbravo.bean.ProductCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.DataRead;
import com.openbravo.data.loader.DataWrite;
import com.openbravo.data.loader.Datas;
import com.openbravo.data.loader.I_Session;
import com.openbravo.data.loader.SerializerReadDouble;
import com.openbravo.data.loader.Session;
import com.openbravo.data.loader.TableDefinition;
import com.openbravo.format.Formats;
import com.openbravo.bean.ProductCustomerCategoryPrice;
import com.openbravo.data.loader.DataParams;
import com.openbravo.data.loader.KeyBuilder;
import com.openbravo.data.loader.PreparedSentence;
import com.openbravo.data.loader.SerializerWriteParams;
import com.openbravo.data.model.Field;
import com.openbravo.pos.bean.ProductExt;

/**
 *
 * @author Administrator
 */
public class ProductCustomerCategoryPriceDAO extends ModifiedLogDAO<ProductCustomerCategoryPrice>{

    CustomerCategoryDAO customerCategoryDAO;

    public ProductCustomerCategoryPriceDAO(I_Session s,CustomerCategoryDAO customerCategoryDAO) {
        super(s);
        this.customerCategoryDAO=customerCategoryDAO;
    }
    
    
    @Override
    public TableDefinition getTable() {
        return new TableDefinition(s,
                "PRODUCT_CUSTOMER_CATEGORY_PRICE",
                new Field[]{
                   new Field(I_ProductCustomerCategoryPrice.ID,Datas.STRING, Formats.STRING),
                   new Field(I_ProductCustomerCategoryPrice.PRODUCT,Datas.STRING, Formats.STRING,Product.class),
                   new Field(I_ProductCustomerCategoryPrice.CATEGORY,Datas.STRING, Formats.STRING,ProductCategory.class),
                   new Field(I_ProductCustomerCategoryPrice.PIRCE, Datas.DOUBLE, Formats.CURRENCY),
                   new Field(I_ProductCustomerCategoryPrice.WHOLESALEPRICE, Datas.DOUBLE, Formats.CURRENCY),
                   new Field(I_ProductCustomerCategoryPrice.LASTMODIFIED,Datas.TIMESTAMP, Formats.TIMESTAMP)
                } ,
                new int[]{0});
    }

    @Override
    public void writeInsertValues(DataWrite dp, ProductCustomerCategoryPrice obj) throws BasicException {
             dp.setString(1, obj.getId());
             dp.setString(2, obj.getProductId());
             dp.setString(3, obj.getCustomerCategory().getID());
             dp.setDouble(4, obj.getM_dCustomerCategoryPrice());
             dp.setDouble(5,obj.getM_dWholesalePrice());
             dp.setTimestamp(6, obj.getLastModified());
    }

    public ProductCustomerCategoryPrice readValues(DataRead dr,ProductCustomerCategoryPrice obj) throws BasicException {
           if(obj==null)
        	   obj=new ProductCustomerCategoryPrice();
          obj.setId(dr.getString(1));
          obj.setProductId(dr.getString(2));
          obj.setCustomerCategory(customerCategoryDAO.find(KeyBuilder.getKey(dr.getString(3))));
          obj.setM_dCustomerCategoryPrice(dr.getDouble(4));
          obj.setM_dWholesalePrice(dr.getDouble(5));
          obj.setLastModified(dr.getTimestamp(6));
          return obj;
    }


    @Override
    public Class getSuportClass() {
        return ProductCustomerCategoryPrice.class;
    }

    public ProductCustomerCategoryPrice findProductCustomerCategoryPrice(final String productId, final String customerCategoryId) throws BasicException {
        return (ProductCustomerCategoryPrice) new PreparedSentence(s,this.getTableDefinition().getListSQL()+" WHERE PRODUCT=? AND CATEGORY=? ", SerializerWriteParams.INSTANCE,this
                ).list(new DataParams() {
                        public void writeValues() throws BasicException {
                           setString(1, productId);
                           setString(2, customerCategoryId);
                }});
    }

    public Double findCustomerCategoryPrice(final String productId, final String customerCategoryId) throws BasicException {
    	return  (Double) new PreparedSentence(s,"SELECT "+I_ProductCustomerCategoryPrice.PIRCE+" FROM PRODUCT_CUSTOMER_CATEGORY_PRICE  WHERE PRODUCT=? AND CATEGORY=? ", SerializerWriteParams.INSTANCE,SerializerReadDouble.INSTANCE
        ).find(new DataParams() {
                public void writeValues() throws BasicException {
                   setString(1, productId);
                   setString(2, customerCategoryId);
        }});
    }
    
    public Double findCustomerCategoryWholesalePrice(final String productId, final String customerCategoryId) throws BasicException {
    	return  (Double) new PreparedSentence(s,"SELECT "+I_ProductCustomerCategoryPrice.WHOLESALEPRICE+" FROM PRODUCT_CUSTOMER_CATEGORY_PRICE  WHERE PRODUCT=? AND CATEGORY=? ", SerializerWriteParams.INSTANCE,SerializerReadDouble.INSTANCE
        ).find(new DataParams() {
                public void writeValues() throws BasicException {
                   setString(1, productId);
                   setString(2, customerCategoryId);
        }});
    }
}
